package it.antonio.ilpizze.reflection;

public class PropertyAccessor {
        
        private String name;
        private MethodAccessor getter;
        private MethodAccessor setter;
        
        
        public PropertyAccessor(String name, MethodAccessor getter,
                        MethodAccessor setter) {
                super();
                this.name = name;
                this.getter = getter;
                this.setter = setter;
        }

        public Object get(Object instance){
                return getter.invoke(instance, new Object[]{});
        }
        
        public void set(Object instance, Object value){
                setter.invoke(instance, value);
        }

        public String getName() {
                return name;
        }
        
        public boolean isWritable(){
        	return setter != null;
        }
        
        public boolean isReadable(){
        	return getter != null;
        }
        
        public Class<?> getType(){
                if(getter!= null){
                        return getter.getReturnType();
                }
                if(setter!= null){
                        return setter.getParameterTypes()[0];
                }
                return null;
        }

		public MethodAccessor getGetter() {
			return getter;
		}

		public MethodAccessor getSetter() {
			return setter;
		}
        
        
}